package com.seth.myapplication.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.seth.myapplication.R;
import com.seth.myapplication.helper.ItemTouchHelperAdapter;

import java.util.Collections;
import java.util.List;

/**
 * Created by Seth on 2017/10/11.
 */

public class BlockAdapter extends RecyclerView.Adapter<BlockAdapter.BlockViewHolder> implements ItemTouchHelperAdapter {

    private List<String> Infos;
    private Context mContext;
    private LayoutInflater mInflater;


    public BlockAdapter(Context context, List<String> infos){
        Infos = infos;
        mContext  = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public BlockViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_block, parent, false);


        return new BlockViewHolder(view);
    }

    @Override
    public void onBindViewHolder(BlockViewHolder holder, int position) {
        holder.mTextView.setText(Infos.get(position));
    }

    @Override
    public int getItemCount() {
        return Infos.size();
    }

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        /**
         * 拖拽后，切换位置，数据排序
         */
        Collections.swap(Infos,fromPosition,toPosition);
        notifyItemMoved(fromPosition,toPosition);

        return true;

    }

    @Override
    public void onItemDismiss(int position) {
        /**
         * 移除之前的数据
         */
        Infos.remove(position);
        notifyItemRemoved(position);

    }

    public static class BlockViewHolder extends RecyclerView.ViewHolder {
        TextView mTextView;

        public BlockViewHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.block_item);
        }
    }
}
